package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 查询通用设置
     * @return
     */
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2.获取用户手机号
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        //3.获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢JAVA吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //4.获取用户的APP通知开关
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }

    /**
     * 查询用户的陌生人问题
     * @param content
     */
    public void saveQuestion(String content) {
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.调用API查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //3.判断问题是否存在
        if (question == null) {
            //3.1如果不存在
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //3.2如果存在
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /**
     * 查询用户的通知开关
     * @param map
     */
    public void saveSettiongs(Map map) {
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.根据用户id查询当前用户的
        Settings settings = settingsApi.findByUserId(userId);
        //3.判断是否存在
        if (settings == null) {
            //3.1如果不存在
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
            //3.2如果存在
        }
    }

    /**
     * 分页查询黑名单列表
     * @param page
     * @param size
     * @return
     */
    public PageResult blacklist(int page, int size) {
        //1.获取当前用户的id
        Long userId = UserHolder.getUserId();
        //2.通过API查询用户的黑名单分页列表
        IPage<UserInfo> iPage = blackListApi.findByUserId(page,size,userId);
        //3.对象转换
        PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords());
        return pr;
    }

    public void deleteBlackList(Long blackUserId) {
        //1.获取当前用户的id
        Long userId = UserHolder.getUserId();
        //
        blackListApi.delete(userId,blackUserId);

    }
}
